package com.ete.service.dialog.version

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.annotation.ColorRes
import com.ete.service.dialog.BaseDialog
import com.ete.service.service.R
import com.ete.service.service.databinding.DialogCheckVersionBinding
import com.ete.service.widget.LoadingView

/**
 * Author:LiXian
 * <p>
 * Desc:检查新版本
 * Version:1.0
 * date 2023/12/19 13:35
 */
class CheckVersionDialog(
        mContext: Context,
        @ColorRes var themeColor: Int = 0,
        var dismissCallback: (() -> Unit)? = null
) : BaseDialog(mContext, R.style.XUIDialog_Custom) {

    private var mTvContent: TextView? = null
    private var mLoadingView: LoadingView? = null

    fun initDialog() {
        setCanceledOnTouchOutside(false)
        setCancelable(false)
        val mBinding = DialogCheckVersionBinding.inflate(LayoutInflater.from(context))
        setContentView(mBinding.root)
        initView(mBinding)
    }

    private fun initView(mBinding: DialogCheckVersionBinding) {
        mTvContent = mBinding.dialogTvContent
        mLoadingView = mBinding.dialogLoadingView
        mTvContent!!.text = context.getString(R.string.version_check_new_version)
        mLoadingView!!.visibility = View.VISIBLE
        mBinding.dialogTvTitle.visibility = View.GONE
        mBinding.dialogBtnProgress.text = context.getString(R.string.version_cancel)
        if (themeColor != 0) {
            mBinding.dialogBtnProgress.updateNormalBgColor(themeColor)
        } else {
            mBinding.dialogBtnProgress.updateNormalBgColor(R.color.color_3F73ED)
        }
        mBinding.dialogBtnProgress.setOnClickListener { dismissDialog() }
        mBinding.dialogIvClose.setOnClickListener { dismissDialog() }
    }

    private fun dismissDialog() {
        mLoadingView?.stop()
        dismiss()
        dismissCallback?.invoke()
    }

    override fun show() {
        super.show()
        mLoadingView?.start()
    }

    override fun getDialogWidthRate(): Float = 0.38F
}